// pages/mine/mine.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid:"",
    content:"登录查看更多内容",
    isTittle:true,
    isLogin:0,
    percentage:"0",
    isVerify:"马上登录",
    tips:"让我们见证您的兼职历程",
    userInfo:{},
    isShow:false,
    trackingList:[
      {
        tittle:"已报名",
        number:0
      },
      {
        tittle: "已录用",
        number: 0
      },
      {
        tittle: "已结算",
        number: 0
      }
    ],
    otherListItems:[
      {
        src:"../../image/add_person.png",
        itemLeftWord:"我要招人",
        itemRightWord:"一招就够"
      },
      {
        src:"../../image/dh.png",
        itemLeftWord: "联系客服",
        itemRightWord: "电话拨打"
      },
      {
        src:"../../image/yijian2.png",
        itemLeftWord: "意见反馈",
        itemRightWord: ""
      }
    ]
  },
  //点击了头像
  onHead(e){
    if (this.data.isLogin == 0) {
      this.showdialog();
    } else {
      wx.navigateTo({
        url: 'myinfo/myinfo',
      })
    }
  },
  //退出
  exit(e){
    this.setData({
      content:"确定退出并解绑吗？",
      isTittle:false,
      isShow: true
    });
  },
  //调出对话框
  showdialog(){
    if (this.data.isLogin == 0) {
      this.setData({
        isShow: true,
        content: "登录查看更多内容",
        isTittle: true,
      });
    }
  },
  //点击马上登录或者未认证
  onVerify(e){
    if (this.data.isLogin == 0) {
      this.showdialog();
    }
    else if(this.data.isVerify=="已认证"){
      wx.navigateTo({
        url: '/pages/pre-info/pre-info',
      })
    }else{
        wx.navigateTo({
          url: '/pages/edit-info/info-base/info-base',
        })
    }
  },
  // 确定按钮
  onConfirmTap(e) {
    if (this.data.isLogin == 0) {
      this.onLogin()
      
    } else if (this.data.isLogin == 1){
      try {
        wx.removeStorageSync('uid')
      } catch (e) {

      }
      this.setData({
        isLogin: 0,
        isShow: false,
        trackingList: [
          {
            tittle: "已报名",
            number: 0
          },
          {
            tittle: "已录用",
            number: 0
          },
          {
            tittle: "已到岗",
            number: 0
          },
          {
            tittle: "已结算",
            number: 0
          }
        ],
        percentage:"0"
      });
    }
  },
  // 取消按钮
  onCancelTap(e) {
    this.setData({
      isShow: false
    });
  },
  //点击马上完善按钮
  fillResume: function(e){
    if (this.data.isLogin == 0) {
      this.showdialog();
    } else {
      wx.navigateTo({
        url: 'myinfo/myinfo',
      })
    }
  },
  //点击其他选项列表相关
  otherList: function(e){
    switch (e.currentTarget.dataset.index) {
      case 0:
        console.log("我要招人")
        wx.navigateTo({
          url: '/pages/list/list',
        })
        break;
        case 1:
          wx.makePhoneCall({
            phoneNumber: '18899889988' //仅为示例，并非真实的电话号码
          })
          break;
        case 2:
          wx.navigateTo({
            url: 'feedback/feedback',
          })
          break;
    }
  },
  // 点击报名跟踪相关
  trackingNumber: function(e){
    console.log(e.target.dataset.index)
    if (this.data.isLogin == 0) {
      this.showdialog();
    }else{
      wx.navigateTo({
        url: 'tracking/tracking?key=' + e.target.dataset.index,
      })
    }
  },
  //登录函数
  onLogin(){
    var _this = this;
    wx.navigateTo({
      url: '/pages/auth/index',
    })
  },

  onShow: function () {
    var that = this
    let userInfo = app.globalData.userInfo;
    this.setData({
      userInfo,
      percentage: userInfo.percentage||0
    })
    let token = app.getCache('token');
    if(!token){
      this.setData({
        isShow: true,
        content: "登录查看更多内容",
        isTittle: true,
      });
    }else{
      this.setData({
        isLogin:1,
        isTittle:true,
        isVerify: "未认证 >",
        tips: "认证需要填写身份证"
      });
      if(userInfo.mainInfo&&userInfo.mainInfo.idCard&&userInfo.mainInfo.sname){
        this.setData({
          isVerify: "已认证",
          tips: ""
        });
      }
        const db = wx.cloud.database();
        let userId = app.getCache('openid');
        const $ = db.command.aggregate
        db.collection('user_job').aggregate().match({userId: userId})
          .bucket({
            groupBy: '$status',
            boundaries: [1, 2, 3, 4],
            default: 'other',
            output: {
              count: $.sum(1),
            }
          })
          .end()
          .then(res=>{
            console.log("报名",res)
            let trackingList = this.data.trackingList;
            res.list.map(el=>{
              if(el._id === 1){
                trackingList[0].number = el.count||0
              }
              if(el._id === 2){
                trackingList[1].number = el.count||0
              }
              if(el._id === 3){
                trackingList[2].number = el.count||0
              }
            })
            this.setData({
              trackingList
            })
          })
      
    }
  },

})